% Matlab code to visualize the equilibrium with firm insolvency

% To run this file seperately, specify the root directly below:
% cd 'C:/Users/Jilun/Dropbox/WorldEconomy_RiskIntolerance/paper RFS final submission/replication/numerical and calibration exercises'


% FIGURE 8
% This is similar to the baseline case with the difference that
% Productivity is now endogenous and given by z0 \bar{S}
% where \bar{S} =gamma + (1-gamma) *F(z0*p/(1-exp(-rho)))
% Here: gamma captures the efficiency of the bankruptcy system
% and F(.) captures the distribution of initial debt/cash across firms
% 
% We assume F(.) is uniformly distributed over [-b,b]
% Therefore, F(\bar{b}) = (\bar{b}+b)/2b



clear all;
global g rho sigma PStar taub tauh k l tau tauBarBench zBarBench zTildeBench
loadVariables; % Sets the above parameters and the baseline shocks

% Set the new parameters

% Productivity shock
% Set it to a relatively mild level for the benchmark to illustrate
% additional damage caused by the debt overhang
z0 = zTildeBench + 0.5*(zBarBench-zTildeBench);

% Distribution of debt
% Set this so that some firms will be insolvent even if demand recession is contained
b = zBarBench/(1 - exp(-rho));

% Efficiency of bankruptcy: gamma
% Set this so that bankruptcy is quite inefficient
gamma = 0;

% Benchmark case without debt overhang (b=0)
eq = solveEquilibriumDebtOverhang(z0,0,0,0);
% Case with debt overhang (b>0) and costly insolvency (gamma=0)
eq_b = solveEquilibriumDebtOverhang(z0,0,gamma,b);

%%% Extract the lines that will be plotted 
pArr = eq.pArr;
sharpeActualArr = eq.sharpeActualArr;
sharpeRequiredArr = eq.sharpeRequiredArr;
SArr = eq.SArr;
SBarArr = eq.SBarArr;
ind = eq.ind;

pArr_b = eq_b.pArr;
sharpeActualArr_b = eq_b.sharpeActualArr;
sharpeRequiredArr_b = eq_b.sharpeRequiredArr;
ind_b = eq_b.ind;
ph_b = eq_b.ph;
SArr_b = eq_b.SArr;
SBarArr_b = eq_b.SArr;

%%%%%%%%%%%% Plot the relevant lines: Plot without and with debt overhang

figure('Position',[30 30 1030 430]);
%%%%% Use the tight subplot command to customize the margins
%   gap- two elements vector [vertical,horizontal] defining the gap between neighbouring axes. Default value
%            is 0.01. Note this vale will cause titles legends and labels to collide with the subplots, while presenting
%            relatively large axis. 
%   marg_h  margins in height in normalized units (0...1)
%            or [lower uppper] for different lower and upper margins 
%   marg_w  margins in width in normalized units (0...1)
%            or [left right] for different left and right margins 
subplot = @(m,n,p) subtightplot (m, n, p, [0.05 0.05], 0.1, 0.05);


% Now plot the equilibrium curves
% First plot the price per productivity (severity of demand recession)
subplot(1,4,[1:3]);
hold on;
h(1) = plot(pArr,sharpeActualArr,'b','LineWidth',3);
h(2) = plot(pArr,sharpeRequiredArr,'r--','LineWidth',3);
% Find and mark the equilibrium
plot(pArr(ind),sharpeActualArr(ind),'ko-','LineWidth',3);


h(3) = plot(pArr_b,sharpeRequiredArr_b,'r','LineWidth',3);
% Find and mark the equilibrium
indEq = find(sharpeActualArr_b - sharpeRequiredArr_b<0,1,'first');
plot(pArr_b(ind_b),sharpeActualArr_b(ind_b),'ko-','LineWidth',3);

titleStr = sprintf('Equilibrium with debt overhang and firm insolvencies');
title(titleStr,'Interpreter','Latex','FontSize',20);

legendStr1 = sprintf('Actual Sharpe ratio, $r^f_0=0$');
legendStr2 = sprintf('Required Sharpe ratio, no debt');
legendStr3 = sprintf('Required Sharpe ratio, firm debt');

hL = legend(h([1 3 2]),legendStr1,legendStr3,legendStr2);
set(hL,'orientation','vertical','fontsize',14,'interpreter','latex');
xlim([ph 1]);
ylim([sharpeActualArr(end) sharpeActualArr(1)]);
xlabel('Normalized price, $p_0$','Interpreter','Latex','FontSize',20);

subplot(1,4,4);
hold on;
% Plot the fraction of solvent firms
plot(pArr,SArr,'r--','LineWidth',3);
plot(pArr(end),1,'ko-','LineWidth',3);

plot(pArr,SArr_b,'r','LineWidth',3);
plot(pArr_b(ind_b),SArr_b(ind_b),'ko-','LineWidth',3);

titleStr = sprintf('Fraction of solvent firms, $S$');
title(titleStr,'Interpreter','Latex','FontSize',20);

xlabel('$p_0$','Interpreter','Latex','FontSize',20);
xlim([ph 1]);

saveas(gcf,[pwd '/figures/fig8.eps'],'epsc');

